package com.csc440.team2.database.entities;

import java.io.Serializable;
import java.util.List;

import android.content.Context;

import com.csc440.team2.database.DBAdapter;

public class Resource implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private long id;
	private long messageID;
	private ResourceType resourceType;
	private String location;
	
	public Resource ()
	{
		setId(-1);
		setMessageId(-1);
		setResourceType(ResourceType.IMAGE);
		setLocation("");
	}
	
	public Resource (long id)
	{
		setId(id);
	}
	
	public Resource (long messageID, ResourceType resourceType, String location)
	{
		setId(-1);
		setMessageId(messageID);
		setResourceType(resourceType);
		setLocation(location);
	}
	
	public Resource (long id, long messageID, ResourceType resourceType, String location)
	{
		setId(id);
		setMessageId(messageID);
		setResourceType(resourceType);
		setLocation(location);
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public long getMessageId() {
		return messageID;
	}
	public void setMessageId(long messageID) {
		this.messageID = messageID;
	}
	public ResourceType getResourceType() {
		return resourceType;
	}
	public void setResourceType(ResourceType resourceType) {
		this.resourceType = resourceType;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	
	private static DBAdapter getDatabase (Context context)
	{
		DBAdapter adapter = new DBAdapter(context);
		adapter.open();
		return adapter;
	}
	
	public boolean load(Context context)
	{
		boolean result;
		DBAdapter adapter = getDatabase(context);
		result = adapter.getResource(this);
		adapter.close();
		return result;
	}
	
	public boolean save (Context context)
	{
		boolean result;
		DBAdapter adapter = getDatabase(context);
		result = adapter.saveResource(this);
		adapter.close();
		return result;
	}
	
	public boolean delete (Context context)
	{
		boolean result;
		DBAdapter adapter = getDatabase(context);
		result = adapter.deleteResource(this);
		adapter.close();
		return result;
	}	
	
	public static List<Resource> getAll (Context context)
	{
		List<Resource> result;
		DBAdapter adapter = getDatabase(context);
		result = adapter.getAllResources();
		adapter.close();
		return result;
	}	
	
	public static List<Resource> getAllByMessageID (Context context, long messageID)
	{
		List<Resource> result;
		DBAdapter adapter = getDatabase(context);
		result = adapter.getResourcesByMessageId(messageID);
		adapter.close();
		return result;
	}	
}
